<?php
namespace Admin\Controller;
use User\Api\UserApi;

header('content-type:text/html;charset=utf8');

class PriceController extends AdminController {
    public function index(){
        $nickname       =   I('nickname');
        $map['status']  =   array('egt',0);
        if(is_numeric($nickname)){
            $map['id|title']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['title']    =   array('like', '%'.(string)$nickname.'%');
        }

        $list   = $this->lists('Price',$map,'id');
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '新增商品属性';
        $this->display();
    }



    public function del(){
        $id = I('get.id');
        $res = D('Price')->where(array('id'=>$id))->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    //  public function add($id=0,$vid=0,$sid=0,$title=0){
    //      if(IS_POST){            
    //          $Price = D("price");
    //          $res = $Price->create(I("post."));
    //          if($res){

    //              if($Price->add($res)){
    //                  $this->success('添加成功',U('index'));
    //              }else{
    //                  $this->error('添加失败');
    //              }
    //          }else{
    //              $this->error(D("price")->getError());
    //          }
    //      }else{
    //          $this->assign('id',$id);
    //          $this->assign('sid',$sid);
    //          $this->assign('vid',$vid);
    //          $this->assign('title',$title);
    //          $this->display();
    //     }
    // }  

    public function add($id=0,$title=0){
        if(IS_GET){
 
           
     $attr= D('attr')->join("onethink_value on onethink_attr.id=onethink_value.kid")->where(array('onethink_attr.sid'=>$id))->select();
    // dump(D('attr')->getLastSql());
    //dump($attr);
        $arr=array();
        $arr1=array();
        foreach($attr as $k=>$v){
                 $arr1[$v['kid']][]=$v['id'];
                $arr[$v['kid']][$v['id']]['vname']=$v['vname']; 
                $arr[$v['kid']][$v['id']]['kname']=$v['kname']; 
                $arr[$v['kid']][$v['id']]['kid']=$v['kid'];
           
        }
             
          // dump($arr1);      
   
        $sets=array_values($arr);
         $sets1=array_values($arr1);
           $result1 = $this->combineDika($sets1);
        
       $result = $this->combineDika($sets);
       //dump($result1);
      for($i=0;$i<count($result);$i++){
        $res[$i]=array_combine($result1[$i],$result[$i]);
      }
       //dump($res);
      
       
            $this->assign('res',$res);  
      
        $this->assign('sid',$id);
        $this->assign('title',$title);
        $this->assign('vname',$vname);
        $this->display();
        //dump($result);
        }
        else{
            $prices = D('price');
            $res = I('post.');
            //dump($res);die;  
                 for($i = 0; $i < count($res['sid']); $i++){
                    $arr = array();
                 foreach($res as $k=>$v){                     
                     $arr[$k] = $v[$i];

                }//die;
                    if($prices->add($arr)){
                        $this->success('添加成功',U('index'));
                    }else{
                        $this->error('添加失败');
                    } 
                 }  
        }
        
    }

     public function combineDika($data)
    {
        $cnt    = count($data);
        $result = array();
        foreach ($data[0] as $item) {
            $result[] = array($item);
        }
        for ($i = 1; $i < $cnt; $i++) {
            $result = $this->combineArray($result, $data[$i]);
        }
        return $result;
    }

    public function combineArray($arr1, $arr2)
    {
        $result = array();
        foreach ($arr1 as $item1) {
            foreach ($arr2 as $item2) {
                $temp     = $item1;
                $temp[]   = $item2;
                $result[] = $temp;
            }
        }
        return $result;
    }



        public function CartesianProduct($sets)
     {
         // 保存结果
         $result = array();
         // 循环遍历集合数据
         for ($i = 0, $count = count($sets); $i < $count - 1; $i++
             ) {
             // 初始化
             if ($i == 0) {
                 $result = $sets[$i];
             }
             // 保存临时数据
             $tmp = array();
             // 结果与下一个集合计算笛卡尔积
             foreach ($result as $res) {
                 foreach ($sets[$i + 1] as $set) {

                     $tmp[] = $res . ',' . $set;
                     dump($tmp);
                 }
             }
             // 将笛卡尔积写入结果
             $result = $tmp;
         }
         return $result;
     }


    public function upload(){
        //dump($_FILES["img"]);die;
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     '/Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        //$upload->saveName  =     '';// 采用时间戳命名
     
        // 上传文件 
        $info   =   $upload->upload();
        //dump($info);
        if(!$info) {// 上传错误提示错误信息
             $this->error($upload->getError());
        }else{// 上传成功
             //$this->success('上传成功！');      
             return $info;
        }
    }

    public function update(){
        if(IS_POST){
            $Price = D("price");
            $img = $this->upload(); 
            //dump($img);
            for($i=0 , $count = count($img) ;$i < $count ; $i++){
                    $tmp = $img[$i];
                    $a.='/Uploads/'.$tmp['savepath'].$tmp['savename'].',';
                    
            } 
            $a=rtrim($a,',');
            $post=I('post.');

            $post['img']=$a;
            //dump($post['img']);die;
            //dump($post);die;
            if($Price->save($post)){             
                $this->success('修改成功',U('index'));
            }else{
                $this->error('修改失败');          
            }
        }else{
            $Price = D("price");
            $res =$Price->where(array('id'=>I("get.id")))->find();
            $this->assign('res',$res);
            $this->assign('img',$img);
            $this->display();
        }
    }


}